<?php

namespace Home\Controller;

use Think\Controller;

class WxshareController extends Controller {
    //获取微信签名所需的access_token
    public function getAccessToken() {
        //查询数据表里面的值
        $info = D('WxToken')->find();
        if($info['expires_in'] < time()){
            //获取token的值
            $url_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('AppID').'&secret='.C('AppSecret');
            $tmp = $this->CURLSend($url_token); //json格式
            $obj = json_decode($tmp);
            if($obj->access_token != null){
                $data['access_token'] = $obj->access_token;
                $data['expires_in'] = time() + $obj->expires_in;
                if($info['id']){
                    $res = D('WxToken')->where('id='.$info['id'])->save($data);
                }else{
                    $res = D('WxToken')->add($data);
                }
                if($res){
                    $data_new = $obj->access_token;
                }else{
                    $data_new = 'error';
                }
                return $data_new;
            }else return 'error';
        }else return $info['access_token'];
    }

    //获取微信签名所需的 ticket
    public function getTicket(){
        $token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
        $tmp = $this->CURLSend($url); //json格式
        $obj = json_decode($tmp);
        return $obj->ticket;
    }


    //整理数据并获取微信签名
    public function generateSign(){
        $noncestr  = uniqid();
        $timestamp = time();
        $url       = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        $ticket    = $this->getTicket();
        if ($ticket) {
            $str = 'jsapi_ticket='.$ticket.'&noncestr='.$noncestr.'×tamp='.$timestamp.'&url='.$url;
            $signature = sha1($str);
            $data['noncestr'] = $noncestr;
            $data['timestamp'] = $timestamp;
            $data['signature'] = $signature;
            $data['appId'] = C('AppID');
            $data['link'] = $url;
            return $data;
        }
    }

    public function index(){
        $wxconfig = wx_share_init();
       print_r($wxconfig);
        $this->assign('wxconfig', $wxconfig);
    }

=======
<?php

namespace Home\Controller;

use Think\Controller;

class WxshareController extends Controller {
    //获取微信签名所需的access_token
    public function getAccessToken() {
        //查询数据表里面的值
        $info = D('WxToken')->find();
        if($info['expires_in'] < time()){
            //获取token的值
            $url_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('AppID').'&secret='.C('AppSecret');
            $tmp = $this->CURLSend($url_token); //json格式
            $obj = json_decode($tmp);
            if($obj->access_token != null){
                $data['access_token'] = $obj->access_token;
                $data['expires_in'] = time() + $obj->expires_in;
                if($info['id']){
                    $res = D('WxToken')->where('id='.$info['id'])->save($data);
                }else{
                    $res = D('WxToken')->add($data);
                }
                if($res){
                    $data_new = $obj->access_token;
                }else{
                    $data_new = 'error';
                }
                return $data_new;
            }else return 'error';
        }else return $info['access_token'];
    }

    //获取微信签名所需的 ticket
    public function getTicket(){
        $token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
        $tmp = $this->CURLSend($url); //json格式
        $obj = json_decode($tmp);
        return $obj->ticket;
    }


    //整理数据并获取微信签名
    public function generateSign(){
        $noncestr  = uniqid();
        $timestamp = time();
        $url       = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        $ticket    = $this->getTicket();
        if ($ticket) {
            $str = 'jsapi_ticket='.$ticket.'&noncestr='.$noncestr.'×tamp='.$timestamp.'&url='.$url;
            $signature = sha1($str);
            $data['noncestr'] = $noncestr;
            $data['timestamp'] = $timestamp;
            $data['signature'] = $signature;
            $data['appId'] = C('AppID');
            $data['link'] = $url;
            return $data;
        }
    }

    public function index(){
        $wxconfig = wx_share_init();
       print_r($wxconfig);
        $this->assign('wxconfig', $wxconfig);
    }

}